package cn.nubia.admin.sysconfig.controller;

import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.springmvc.Response;
import cn.nubia.admin.sysconfig.bean.DictField;
import cn.nubia.admin.sysconfig.bean.DictFieldItem;
import cn.nubia.admin.sysconfig.query.DictFielItemdQuery;
import cn.nubia.admin.sysconfig.service.DictFieldItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Administrator on 2015/7/8.
 */
@Controller
@RequestMapping("/admin/system/dictFieldItem")
public class DictFieldItemController {
    private final static Logger logger = LoggerFactory.getLogger(DictFieldItemController.class);
    @Autowired
    private DictFieldItemService dictFieldItemService;
    /**
     * 新增枚举接口
     *
     * @param dictFieldItem 枚举对象
     * @return 新增的枚举对象.
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Response<DictFieldItem>   saveDictField(@Valid DictFieldItem dictFieldItem,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.getFailedResponse(bindingResult.getFieldError().getDefaultMessage());
        }
        dictFieldItemService.save(dictFieldItem);
        return Response.getSuccessResponse(dictFieldItem);
    }
    /**
     * 新增枚举接口
     *
     * @return 新增的枚举对象.
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
  //  @RequiresPermissions(value = "dictFieldItem:add")
    public ModelAndView addDictField(Integer fieldId) {
        ModelAndView modelAndView = new ModelAndView("/system/dictFieldItemForm");
        DictFieldItem dictFieldItem =new DictFieldItem();
        dictFieldItem.setFieldId(fieldId);
        modelAndView.addObject("dictFieldItem",dictFieldItem);
        return modelAndView;
    }
    /**
     * 查看枚举
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
   // @RequiresPermissions(value = "dictFieldItem:edit")
    public ModelAndView view(Integer id) {
        ModelAndView modelAndView = new ModelAndView("/system/dictFieldItemForm");
        DictFieldItem DictFieldItem =null;
        try {
            DictFieldItem= dictFieldItemService.view(id);
            modelAndView.addObject("dictFieldItem",DictFieldItem);
        }  catch (Exception e) {
            logger.error("查看枚举异常:参数"+id, e);
        }
        return modelAndView;
    }

    /**
     * 枚举列表
     * @param dictFieldItemQuery
     * @return
     */
    @RequiresPermissions("soft:appcfg:dict:dictFieldItem:list")
    @RequestMapping(value ="/list", method = RequestMethod.GET)
    public ModelAndView list(DictFielItemdQuery dictFieldItemQuery) {
        //参数不正确
        Paged<DictFieldItem> dictFieldItemPaged=null;
        ModelAndView modelAndView = new ModelAndView("/system/dictFieldItemList");
        try {
            dictFieldItemPaged=dictFieldItemService.list(dictFieldItemQuery);
           List<DictField> dictFieldList= dictFieldItemService.getDictFieldList();
            modelAndView.addObject("query",dictFieldItemQuery);
            modelAndView.addObject("pageList",dictFieldItemPaged);
            modelAndView.addObject("dictFieldList",dictFieldList);
        }  catch (Exception e) {
            logger.error("枚举查询异常:参数:"+dictFieldItemQuery.toString(), e);
            // return Response.getFailedResponse("枚举查询失败", null);
        }
        return modelAndView;
    }

    /**
     * 删除枚举
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value = "dictFieldItem:delete")
    public Response<Boolean> delete(Integer id) {
        boolean result=false;
        try {
            dictFieldItemService.delete(id);
        }  catch (Exception e) {
            logger.error("删除枚举异常:参数"+id, e);
            return Response.getFailedResponse("删除失败",false);
        }
        return Response.getSuccessResponse(true);
    }

    /**
     * 校验枚举
     * @param id
     * @param fieldId
     * @param itemKey
     * @param itemValue
     * @param itemTitle
     * @return
     */
    @RequestMapping(value = "/check", method = RequestMethod.GET)
    @ResponseBody
    public Boolean check(Integer id,Integer fieldId,String itemKey,String itemValue,String itemTitle) {
        boolean result=false;
        try {
            result= dictFieldItemService.check(id,fieldId,itemKey,itemValue,itemTitle);
        }  catch (Exception e) {
            logger.error("校验枚举异常:参数"+id, e);
            return false;
        }
        return result==true?false:true;
    }
}
